local assets =
{
    Asset("ANIM", "anim/backpack.zip"),
    Asset("ANIM", "anim/swap_krampus_sack.zip"),
}

local function onequip(inst, owner)
end

local function onunequip(inst, owner)
end
--[[
local function onequiptomodel(inst, owner)
    inst.components.container:Close(owner)
end
]]

local function ongetio(owner, data)
    local io = data.item 
    if not io:HasTag("kkd_implant") or not io._onequip then return end 

    if not owner.components.inventoryitem:GetGrandOwner() then 
        local i = #owner.onequipdirty + 1
        owner.onequipdirty[i] = {fn = io._onequip, inst = io}
    else
    
    --owner:DoTaskInTime(0, function()
        io:_onequip(owner, owner.components.inventoryitem:GetGrandOwner())
        --[[if owner.kkd_currenthealth then
            owner.components.health:SetCurrentHealth(owner.kkd_currenthealth)
            owner.components.health:DoDelta(0)
            owner.kkd_currenthealth = nil
        end
        if owner.kkd_currenthunger then
            owner.components.hunger.current = owner.kkd_currenthunger
            owner.components.hunger:DoDelta(0)
            owner.kkd_currenthunger = nil
        end
        if owner.kkd_currentsanity then
            owner.components.sanity.current = owner.kkd_currentsanity
            owner.components.sanity:DoDelta(0)
            owner.kkd_currentsanity = nil
        end]]
    --end)
    end
end

local function onloseio(owner, data)
    local io = data.prev_item 
    if not io:HasTag("kkd_implant") or not io._onunequip then return end 
    --owner:DoTaskInTime(0, function()
    if not owner.components.inventoryitem:GetGrandOwner() then return end
    io:_onunequip(owner, owner.components.inventoryitem:GetGrandOwner())
    --end)
end

local function onpreload(inst, data)
    inst.onequipdirty = {}
end

local function fn_common()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("backpack1")
    inst.AnimState:SetBuild("swap_krampus_sack")
    inst.AnimState:PlayAnimation("anim")

    --inst.foleysound = "dontstarve/movement/foley/krampuspack"

    return inst
end

local function fn_mastersim(inst, widgetsetupname)
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.cangoincontainer = false
    inst.components.inventoryitem.keepondeath = true -- 死亡不掉落
    inst.components.inventoryitem.keepondrown = true -- 落水不掉落

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.KKD_FLESH
    inst.components.equippable:SetPreventUnequipping(true)
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("container")
    inst.components.container:WidgetSetup(widgetsetupname)
    inst.components.container.skipopensnd = true
    inst.components.container.skipclosesnd = true
    inst.components.container.stay_open_on_hide = true

    MakeHauntableLaunchAndDropFirstItem(inst)
end

local function fn1()
    local inst = fn_common()

    inst:AddTag("kkd_fleshsack")

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        inst.OnEntityReplicated = function(inst)
            inst.replica.container:WidgetSetup('kkd_fleshsack') 
        end
        return inst
    end

    fn_mastersim(inst, "kkd_fleshsack")

    inst:ListenForEvent("itemget", ongetio)
    inst:ListenForEvent("itemlose", onloseio)
    
	inst.OnPreLoad = onpreload

    return inst
end

return Prefab("kkd_fleshsack", fn1, assets)
